package com.sskj.gocoin.v.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.util.LanguageUtil;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.rxbus2.RxBus;
import com.sskj.gocoin.mine.VerifyHomeActivity;
import com.sskj.gocoin.p.base.BasePresenter;
import com.sskj.gocoin.v.activity.VerifyJuniorActivity;
import com.sskj.lib.RConfig;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.sskj.gocoin.common.Constants.VERIFY_IDENT_CODE;

/**
 * Created by Lee on 2018/2/28 0028.
 */

public abstract class BaseFragment<T extends BaseActivity, P extends BasePresenter> extends Fragment implements IBaseView {

    protected T mActivity;
    protected P mPresenter;
    private Unbinder mUnbinder;
    private View inflate;

    public boolean isEnglish;
    private FragmentActivity activity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (inflate == null) {
            inflate = inflater.inflate(getLayoutId(), null);
            try {
                mActivity = (T) getActivity();
                activity = getActivity();
            } catch (Exception e) {

            }

            if ("en".equals(MyAppLication.getLANGUAGE())) {
                isEnglish = true;
            } else {
                isEnglish = false;
            }
            mPresenter = getPresenter();
            mPresenter.attachView(this, mActivity);
            mUnbinder = ButterKnife.bind(this, inflate);
            initView();
            initData();
        }
        if (inflate.getParent() != null) {
            ((ViewGroup) inflate.getParent()).removeView(inflate);
        }
        return inflate;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            mPresenter.detachView();
            mUnbinder.unbind();
            RxBus.getDefault().unregister(this);
        } catch (Exception e) {

        }

    }

    public abstract int getLayoutId();

    public abstract P getPresenter();

    public void initView() {
    }

    public void initData() {
    }


    public void setText(TextView view, String text) {
        if (view != null && text != null) {
            view.setText(text);
        }
    }
//    /**
//     * 判断用户实名认证
//     */
//    protected boolean checkIdent() {
//        if (MyAppLication.getRealnameStatus() == 3) {//已实名认证
//            return true;
//        } else if (MyAppLication.getRealnameStatus() == 2) {//审核中
//            showApplyTip(App.INSTANCE.getString(R.string.strBaseFragment200), false);
//            return false;
//        } else if (MyAppLication.getRealnameStatus() == 4) {//被拒绝
//            showApplyTip(App.INSTANCE.getString(R.string.strBaseFragment201), true);
//            return false;
//        } else {
//            showApplyTip(App.INSTANCE.getString(R.string.strBaseFragment202), true);
//            return false;
//        }
//    }

    /**
     * 判断用户初级实名认证
     */
    protected boolean checkIdent(Context context) {

        if (MyAppLication.getRealnameStatus() == 3) {//已实名认证
            return true;
        } else if (MyAppLication.getRealnameStatus() == 2) {//审核中
            showApplyTip(getContext(), App.INSTANCE.getString(R.string.strBaseActivity201), false);
            return false;
        } else if (MyAppLication.getRealnameStatus() == 4) {//被拒绝
            VerifyHomeActivity.start(getContext());
            return false;
        } else {
            showApplyTip(getContext(), App.INSTANCE.getString(R.string.strBaseActivity204), true);
            return false;
        }

    }

    /**
     * 判断用户高级实名认证
     */
    protected boolean checkIdentHign(Context context) {
        if (MyAppLication.getRealnameStatusHigh() == 3) {//已实名认证
            return true;
        } else if (MyAppLication.getRealnameStatusHigh() == 2) {//审核中
            VerifyHomeActivity.start(getContext());
            return false;
        } else if (MyAppLication.getRealnameStatusHigh() == 4) {//被拒绝
            VerifyHomeActivity.start(getContext());
            return false;
        } else {
            VerifyHomeActivity.start(getContext());
            return false;
        }
    }

    /**
     * 判断用户实名认证弹窗
     */
    private void showApplyTip(Context mcontext, String msg, boolean status) {
        AlertDialog tipDialog = new AlertDialog.Builder(mcontext)
                .setTitle(App.INSTANCE.getString(R.string.strBaseFragment210))
                .setMessage(msg)
                .setPositiveButton(status ? App.INSTANCE.getString(R.string.strBaseFragment211) : App.INSTANCE.getString(R.string.strBaseFragment212), (dialog, which) -> {
                    dialog.dismiss();
                    if (status) {
                        openActivity(VerifyJuniorActivity.class, VERIFY_IDENT_CODE);
                    }
                })
                .setNegativeButton(App.INSTANCE.getString(R.string.strBaseFragment213), (dialog, which) -> dialog.dismiss())
                .create();
        tipDialog.show();

    }

    protected void openActivity(Class<? extends Activity> clz, int requestCode) {
        if (MyAppLication.isLogin()) {
            startActivityForResult(new Intent(getActivity(), clz), requestCode);
        } else {
            ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
            getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);

        }
    }

}
